package org.martin.snsrs.client.services.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import android.webkit.MimeTypeMap;

public class RequestParams {
	private static String ENCODING = "UTF-8";
	protected ConcurrentHashMap<String, FileWrapper> fileParams;
	protected ConcurrentHashMap<String, String> urlParams;

	public RequestParams() {
		init();
	}

	public RequestParams(String paramString1, String paramString2) {
		init();
		put(paramString1, paramString2);
	}

	public RequestParams(Map<String, String> paramMap) {
		init();
		Iterator<Entry<String, String>> localIterator = paramMap.entrySet()
				.iterator();
		while (localIterator.hasNext()) {
			Map.Entry<String, String> localEntry = (Map.Entry<String, String>) localIterator
					.next();
			put((String) localEntry.getKey(), (String) localEntry.getValue());
		}
	}

	private void init() {
		this.urlParams = new ConcurrentHashMap<String, String>();
		this.fileParams = new ConcurrentHashMap<String, FileWrapper>();
	}

	public HttpEntity getEntity() {
		if (!this.fileParams.isEmpty()) {
			SimpleMultipartEntity localSimpleMultipartEntity = new SimpleMultipartEntity();
			Iterator<Entry<String, String>> urlItor = this.urlParams.entrySet()
					.iterator();
			while (urlItor.hasNext()) {
				Map.Entry<String, String> entry = (Map.Entry<String, String>) urlItor
						.next();
				localSimpleMultipartEntity.addPart((String) entry.getKey(),
						(String) entry.getValue());
			}
			int i = 0;
			int j = this.fileParams.entrySet().size() - 1;
			Iterator<Entry<String, FileWrapper>> fileIterator = this.fileParams
					.entrySet().iterator();
			boolean isBoundry = true;
			while (fileIterator.hasNext()) {
				Map.Entry<String, FileWrapper> entry = (Map.Entry<String, FileWrapper>) fileIterator.next();
				FileWrapper localFileWrapper = (FileWrapper) entry.getValue();
				if (localFileWrapper.inputStream != null) {
					isBoundry = true;
					if (localFileWrapper.fileName != null)
						localSimpleMultipartEntity.addPart(
								(String) entry.getKey(),
								localFileWrapper.getFileName(),
								localFileWrapper.inputStream,
								localFileWrapper.contentType, isBoundry);
				}
				i++;
				if (i >= j)
					isBoundry = false;
				localSimpleMultipartEntity.addPart(
						(String) entry.getKey(),
						localFileWrapper.getFileName(),
						localFileWrapper.inputStream, isBoundry);
			}
			return localSimpleMultipartEntity;
		}
		try {
			UrlEncodedFormEntity localUrlEncodedFormEntity = new UrlEncodedFormEntity(
					getParamsList(), ENCODING);
			return localUrlEncodedFormEntity;
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
			localUnsupportedEncodingException.printStackTrace();
		}
		return null;
	}

	public String getParamString() {
		return URLEncodedUtils.format(getParamsList(), ENCODING);
	}

	protected List<BasicNameValuePair> getParamsList() {
		LinkedList<BasicNameValuePair> localLinkedList = new LinkedList<BasicNameValuePair>();
		Iterator<Entry<String, String>> localIterator = this.urlParams.entrySet().iterator();
		while (localIterator.hasNext()) {
			Map.Entry<String, String> localEntry = (Map.Entry<String, String>) localIterator.next();
			localLinkedList.add(new BasicNameValuePair((String) localEntry
					.getKey(), (String) localEntry.getValue()));
		}
		return localLinkedList;
	}

	public void put(String paramString, File paramFile)
			throws FileNotFoundException {
		String contentType = "application/octet-stream";
		put(paramString, new FileInputStream(paramFile), paramFile.getName(), contentType);
	}

	public void put(String keyValue, InputStream fileInputStream,
			String fileName, String contentType) {
		if ((keyValue != null) && (fileInputStream != null))
			this.fileParams.put(keyValue, new FileWrapper(fileInputStream,
					fileName, contentType));
	}

	public void put(String paramString1, String paramString2) {
		if ((paramString1 != null) && (paramString2 != null))
			this.urlParams.put(paramString1, paramString2);
	}

	public void remove(String paramString) {
		this.urlParams.remove(paramString);
		this.fileParams.remove(paramString);
	}

	public String toString() {
		StringBuilder localStringBuilder = new StringBuilder();
		Iterator<Entry<String, String>> localIterator1 = this.urlParams.entrySet().iterator();
		while (localIterator1.hasNext()) {
			Map.Entry<String, String> localEntry2 = (Map.Entry<String, String>) localIterator1.next();
			if (localStringBuilder.length() > 0)
				localStringBuilder.append("&");
			localStringBuilder.append((String) localEntry2.getKey());
			localStringBuilder.append("=");
			localStringBuilder.append((String) localEntry2.getValue());
		}
		Iterator<Entry<String, FileWrapper>> localIterator2 = this.fileParams.entrySet().iterator();
		while (localIterator2.hasNext()) {
			Map.Entry<String, FileWrapper> localEntry1 = (Map.Entry<String, FileWrapper>) localIterator2.next();
			if (localStringBuilder.length() > 0)
				localStringBuilder.append("&");
			localStringBuilder.append((String) localEntry1.getKey());
			localStringBuilder.append("=");
			localStringBuilder.append("FILE");
		}
		return localStringBuilder.toString();
	}

	private static class FileWrapper {
		public String contentType;
		public String fileName;
		public InputStream inputStream;

		public FileWrapper(InputStream input, String fileName,
				String contentType) {
			this.inputStream = input;
			this.fileName = fileName;
			this.contentType = contentType;
		}

		public String getFileName() {
			if (this.fileName != null)
				return this.fileName;
			return "nofilename";
		}
	}
}